/*
 * #depends subsonic-application.js
 */
Ext.regController('Ext.ux.mattgoldspink.subsonic.controllers.SongView', {
	initLeftBar: function(options) {
		this.leftBarList = options.leftBar;
	},
    loadFolder: function(options) { 
        var id, type, store, idProp = 'id';
		if (Ext.isDefined(options.historyUrl)) {
			id = options.historyUrl.split('/')[2];
			type = options.historyUrl.split('/')[1];
		}
        store = this.getStore();
        if ((options.initComplete || this.initComplete) && Ext.isDefined(store)) {
            if (type === 'musicDirectory' || type === 'indexes') {
                store.setupMusicDirectoryCall();
            } else if (type === 'playlist') {
				store.setupPlaylistCall();
				if (!Ext.isDefined(this.task)) {
					this.task = new Ext.util.DelayedTask(this.setSelectedMenuItem(id), this);
				}
				this.task.delay(50);
			} else {
				store.setupIndexesCall();
				idProp = 'musicFolderId';
				if (!Ext.isDefined(this.task)) {
					this.task = new Ext.util.DelayedTask(this.setSelectedMenuItem(id), this);
				}
				this.task.delay(50);
			}
            store.getProxy().extraParams[idProp]=id;
            store.clearFilter();
            store.load();
            this.initComplete = true;
        }
    },
	setSelectedMenuItem: function(id) {
		return function(){
			var leftStore = this.getLeftBarStore(), rec = leftStore.getAt(leftStore.find('id', id));
			if (!Ext.isEmpty(rec)){
				this.leftBarList.getSelectionModel().select(rec);
				delete this.task;
			} else {
				this.task.delay(50);
			}
		};
	},
    getStore: function(){
        if(!Ext.isDefined(this.store)) {
            this.store = Ext.StoreMgr.get('Ext.ux.mattgoldspink.subsonic.stores.MusicStore');
        }
        return this.store;
    },
	getLeftBarStore: function(){
        if(!Ext.isDefined(this.leftBarStore)) {
            this.leftBarStore = Ext.StoreMgr.get('Ext.ux.mattgoldspink.subsonic.stores.LeftMenuStore');
        }
        return this.leftBarStore;
	}
});
